package com.example.news.adapter;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.example.news.R;
import com.example.news.activity.WebActivity;
import com.example.news.bean.NewsListJson;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/5/18.
 */
public class NewsListAdapter extends BaseAdapter{
    private FragmentActivity context;
    private ArrayList<NewsListJson.MyResult> list;
    private final DisplayImageOptions options;
    private View view;
    private UMShareListener umShareListener;

    public NewsListAdapter(FragmentActivity context, ArrayList<NewsListJson.MyResult> list, DisplayImageOptions options) {
        this.context=context;
        this.list=list;
        this.options=options;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        /**
         * ListView优化
         */
        view = null;
        ViewHolder holder=null;
        if (convertView==null){
            //如果是第一次的话,就创建布局
            view =View.inflate(context, R.layout.content_list,null);
            holder=new ViewHolder();
            //找控件
            holder.newsImage= (ImageView) view.findViewById(R.id.news_image);
            holder.newsTitle= (TextView) view.findViewById(R.id.news_title);
            holder.newsDescription= (TextView) view.findViewById(R.id.news_description);
            holder.newsTime= (TextView) view.findViewById(R.id.news_time);
            holder.pop_logo= (ImageView) view.findViewById(R.id.pop_logo);
            holder.share= (ImageView) view.findViewById(R.id.share);

            //为holder设置内容
            view.setTag(holder);
        }else{
            //如果不是第一次的话,就复用布局
            view =convertView;
            //取出holder里面的值
            holder = (ViewHolder) view.getTag();
        }
        //设置图片
        ImageLoader.getInstance().displayImage(list.get(position).picUrl, holder.newsImage, options);
        //为控件赋值
        holder.newsTitle.setText(list.get(position).title);
        holder.newsDescription.setText(list.get(position).description);
        holder.newsTime.setText(list.get(position).ctime);

        //为泡泡按钮设置监听
        holder.pop_logo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //加载布局文件
                View popup=View.inflate(context, R.layout.pop_item, null);

                ImageView popClose = (ImageView) popup.findViewById(R.id.pop_close);//关闭泡泡窗口的按钮图片
                TextView read = (TextView) popup.findViewById(R.id.pop_read);//读文章的按钮

                //设置监听-----读文章
                read.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //跳转到新闻详情界面
                        Intent intent = new Intent(context, WebActivity.class);
                        intent.putExtra("url", list.get(position).url);
                        intent.putExtra("title", list.get(position).title);
                        context.startActivity(intent);
                    }
                });

                final PopupWindow window=new PopupWindow(popup, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT,true);

                //设置泡泡显示后，点击屏幕其他地方让其消失
                window.setOutsideTouchable(true);
                //设置背景
                window.setBackgroundDrawable(new BitmapDrawable());
                int[] arrayOfInt = new int[2];
                //获取点击按钮的坐标
                v.getLocationOnScreen(arrayOfInt);
                int x = arrayOfInt[0];
                int y = arrayOfInt[1];
                //设置泡泡窗口显示的位置
                window.showAtLocation(v, 0, x, y);

                popClose.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        window.dismiss();//取消泡泡窗口
                    }
                });
            }
        });

        umShareListener = new UMShareListener() {
            @Override
            public void onResult(SHARE_MEDIA platform) {
                Toast.makeText(context, platform + " 分享成功啦", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(SHARE_MEDIA platform, Throwable t) {
                Toast.makeText(context, platform + " 分享失败啦", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancel(SHARE_MEDIA platform) {
                Toast.makeText(context, platform + " 分享取消了", Toast.LENGTH_SHORT).show();
            }
        };
        /**
         * 分享按钮
         * 实现分享功能
         */
        holder.share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final SHARE_MEDIA[] displaylist = new SHARE_MEDIA[]
                        {
                                SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE,SHARE_MEDIA.SINA,
                                SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE,SHARE_MEDIA.DOUBAN
                        };

                new ShareAction(context).setDisplayList( displaylist )
                        .withText(list.get(position).title)//内容
                        .withTitle(list.get(position).title)//标题
                        .withTargetUrl(list.get(position).url)//地址
                        .withMedia(new UMImage(context,list.get(position).picUrl))//图片
                        .setListenerList(umShareListener)
                        .open();
            }
        });


        return view;
    }

    class ViewHolder{
        ImageView newsImage;//新闻图片
        TextView newsTitle;//新闻标题
        TextView newsDescription;//新闻来源
        TextView newsTime;//新闻时间
        ImageView pop_logo;//泡泡窗
        ImageView share;//分享图标
    }
}
